package com.supersit.greensproject.AsyncTask;

import android.content.Context;
import android.os.AsyncTask;

import com.supersit.greensproject.DB.XUtilsDbManager;
import com.supersit.greensproject.R;
import com.supersit.greensproject.entity.BaseBase;
import com.supersit.greensproject.entity.MaketSubject;
import com.supersit.greensproject.utils.L;
import com.supersit.greensproject.utils.T;
import com.supersit.greensproject.view.ShowDialog;

import org.xutils.DbManager;
import org.xutils.ex.DbException;

import java.util.List;

/**
 * Created by Administrator on 2016/8/25 0025.
 */
public class BaseBaseAndMaketDBUpdate {

    /**
     * 更新数据库
     *
     * @param baseList
     * @param maketList
     */

    public static void updateDB(final List<BaseBase> baseList, final List<MaketSubject> maketList, final Context context) {
        final int SAVE_SUCCESS = 0;
        final int SAVE_FAIL = 1;
        final int NO_DATA = 3;
        final ShowDialog showDialog = new ShowDialog(context);
        final DbManager dbManager = XUtilsDbManager.getInstance();
        new AsyncTask<String, Integer, Integer>() {

            @Override
            protected void onPreExecute() {

                showDialog.showLoadingDialog(context.getString(R.string.saveing));
            }

            @Override
            protected void onPostExecute(Integer s) {
                showDialog.dismissLoadDialog();
                if (s == SAVE_SUCCESS) {
                    String str = context.getString(R.string.update_base_and_maket_ok);
                    T.showShort(context, String.format(str, baseList == null ? "0" : baseList.size() + "", maketList == null ? "0" : maketList.size() + ""));
                } else if (s == SAVE_FAIL) {
                    T.showShort(context, R.string.save_fail);
                }
            }

            @Override
            protected Integer doInBackground(String... params) {
                if (null != baseList && baseList.size() > 0) {
                    for (int i = 0; i < baseList.size(); i++) {////操作状态(0：删除；1：新增；2：修改,-1正常)
                        BaseBase b = baseList.get(i);
                        try {
                            BaseBase baseBase = dbManager.selector(BaseBase.class).where("baseId", "=", b.getBaseId()).findFirst();
                            int id = 0;
                            if (null != baseBase) {
                                id = baseBase.getId();
                            }
                            if (b.getOperationsTatus() == 0) {
                                b.setOperationsTatus(-1);
                                if (id > 0) {
                                    L.d("删除操作，数据库存在 id:" + id);
                                    b.setId(id);
                                    dbManager.update(b);
                                } else {
                                    L.d("删除操作，数据库不存在");
                                    dbManager.saveBindingId(b);
                                }
                            } else if (b.getOperationsTatus() == 1) {
                                L.d("新增操作，");
                                b.setOperationsTatus(-1);
                                dbManager.saveBindingId(b);
                            } else if (b.getOperationsTatus() == 2) {
                                b.setOperationsTatus(-1);
                                if (id > 0) {
                                    L.d("修改操作，数据库存在 id:" + id);
                                    b.setId(id);
                                    dbManager.update(b);
                                } else {
                                    dbManager.saveBindingId(b);
                                    L.d("修改操作，数据库不存在");
                                }
                            }
                        } catch (DbException e) {
                            L.e("数据库操作异常：" + e.getMessage());
                            e.printStackTrace();
                            return SAVE_FAIL;
                        }
                    }
                }
                if (null != maketList && maketList.size() > 0) {
                    for (int i = 0; i < maketList.size(); i++) {////操作状态(0：删除；1：新增；2：修改,-1正常)
                        MaketSubject ms = maketList.get(i);
                        try {
                            MaketSubject maketSubject = dbManager.selector(MaketSubject.class).where("maketId", "=", ms.getMaketId()).findFirst();
                            int mid = 0;
                            if (null != maketSubject) {
                                mid = maketSubject.getId();
                            }
                            if (null != ms) {
                                if (ms.getOperationsTatus() == 0) {
                                    ms.setIsDelete(0);
                                    ms.setOperationsTatus(-1);
                                    if (mid > 0) {
                                        ms.setId(mid);
                                        dbManager.update(ms);
                                    } else {
                                        dbManager.saveBindingId(ms);
                                    }
                                } else if (ms.getOperationsTatus() == 1) {
                                    ms.setOperationsTatus(-1);
                                    dbManager.saveBindingId(ms);
                                } else if (ms.getOperationsTatus() == 2) {
                                    ms.setOperationsTatus(-1);
                                    if (mid > 0) {
                                        ms.setId(mid);
                                        dbManager.update(ms);
                                    } else {
                                        dbManager.saveBindingId(ms);
                                    }
                                }
                            }
                        } catch (DbException e) {
                            L.e("数据库操作异常：" + e.getMessage());
                            e.printStackTrace();
                            return SAVE_FAIL;
                        }
                    }
                }
                return SAVE_SUCCESS;
            }
        }.execute();
    }

}
